package pub.cleangao.mqtt;

import lombok.Getter;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import pub.cleangao.common.event.EventBus;
import pub.cleangao.common.event.ReactEvent;

import java.util.function.Consumer;

@Component
public class LocalMqttClient {


    @Autowired
    private MqttServer mqttServer;

    @Getter
    private MqttClient client;


    public MqttClient connectToServer(MqttServer mqttServer) throws MqttException {
        //创建MQTT客户端对象
        client = new MqttClient("tcp://" + mqttServer.getHost(), mqttServer.getClientId(), new MemoryPersistence());

        //连接设置
        MqttConnectOptions options = new MqttConnectOptions();

        //是否清空session，设置为false表示服务器会保留客户端的连接记录，客户端重连之后能获取到服务器在客户端断开连接期间推送的消息
        //设置为true表示每次连接到服务端都是以新的身份
        options.setCleanSession(true);

        //设置连接用户名
        options.setUserName(mqttServer.getUsername());

        //设置连接密码
        options.setPassword(mqttServer.getPassword().toCharArray());

        //设置超时时间，单位为秒
        options.setConnectionTimeout(120);

        //设置心跳时间 单位为秒，表示服务器每隔1.5*20秒的时间向客户端发送心跳判断客户端是否在线
        options.setKeepAliveInterval(20);

        //设置遗嘱消息的话题，若客户端和服务器之间的连接意外断开，服务器将发布客户端的遗嘱信息
        options.setWill("willTopic", (mqttServer.getClientId() + "与服务器断开连接").getBytes(), 0, false);

        //设置回调
        client.setCallback(new MqttMessageListener(mqttServer));

        client.connect(options);

        //订阅主题
        //消息等级，和主题数组一一对应，服务端将按照指定等级给订阅了主题的客户端推送消息
        // int[] qos = {1, 1};
        //主题
        // String[] topics = {"topic1", "topic2"};

        //订阅主题
        // client.subscribe(topics, qos);
        return client;
    }


}
